<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <form class="layui-form">
        <div class="layui-form lay-header">
          <div class="toolbar">
            <div class="layui-form-item lay-query-content">
              <div class="lay-query-box">
                <div class="query-lt">
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">IQC任务编号：</label>
                    <div class="layui-input-inline">
                      <input name="code" class="layui-input" type="text" placeholder="请输入任务编号" />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">单据编号：</label>
                    <div class="layui-input-inline">
                      <input name="orderCode" class="layui-input" type="text" placeholder="请输入单据编号" />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">来源：</label>
                    <div class="layui-input-inline">
                      <select name="sourceType">
                        <option value="">请选择来源</option>
                        <option value="1">按采购订单</option>
                        <option value="2">按供应商发货物流</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">质检分类：</label>
                    <div class="layui-input-inline">
                      <select id="qualityTestingCategoryId" name="qualityTestingCategoryId" lay-filter="qualityTestingCategoryId" class="common-select-search"></select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">供应商：</label>
                    <div class="layui-input-inline">
                      <select id="supplierId" name="supplierId" lay-filter="supplierId" class="common-select-search"></select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">创建时间：</label>
                    <div class="layui-input-inline">
                      <input name="createTime" id="createTime" class="layui-input date-icon" type="text" placeholder="请选择创建时间" style="width: 200px" readonly />
                    </div>
                    <input name="startCreateTime" id="sCreateTime" type="hidden" />
                    <input name="endCreateTime" id="eCreateTime" type="hidden" />
                  </div>
                </div>
                <div class="query-rt">
                  <div class="layui-inline">
                    <button type="button" class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit type="button">重置</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- <div class="lay-btn-rows">
            <div class="lay-btn-box">
              <div class="lay-btn-lt">
                <button id="purQualityTaskDetailBtnAdd" class="layui-btn icon-btn color-reseda" type="button">添加IQC质检任务</button>
              </div>
              <div class="lay-btn-rt"></div>
            </div>
          </div> -->
        </div>
      </form>
      <!-- 表格 -->
      <table class="layui-table" id="purQualityTaskDetailTable" lay-filter="purQualityTaskDetailTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purQualityTaskDetailTableBar">
  <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>
</script>
<script type="text/html" id="purQualityTaskCheckForm">
  <form lay-filter="purQualityTaskCheckForm" class="layui-form open-form purQualityTaskCheckForm">
    <div class="open-model">
      <div class="open-auto">
        <div class="layui-form-item">
          <div class="layui-form-item">
            <table class="layui-table" id="myPurQualityTaskDetailTable2" lay-filter="myPurQualityTaskDetailTable2"></table>
          </div>
          <div class="layui-form-item">
            <!-- 质检结果 -->
            <table class="layui-table" id="purQualityTaskCheckResultTable2" lay-filter="purQualityTaskCheckResultTable2"></table>
          </div>
        </div>
      </div>
    </div>
  </form>
</script>
<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin", "laydate"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var laydate = layui.laydate;

    form.render("select");

    // 渲染表格
    var insTb = table.render({
      elem: "#purQualityTaskDetailTable",
      url: config.base + "enterprise/purchase/admin/purQualityTaskDetail/list",
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers", title: "序号" },
          { field: "detailCode", title: "IQC取样样品编号" },
          { field: "taskCode", title: "IQC任务编号" },
          { field: "orderCode", title: "物流记录编号" },
          { field: "orderCode", title: "采购单编号" },
          { field: "supplierName", title: "供应商" },
          { field: "skuCode", title: "料号" },
          { field: "skuName", title: "品名" },
          { field: "qualityTestingCategoryName", title: "质检分类" },
                  { field: "qualityTestingTypeName", title: "质检性质" },
                  { field: "qcQualityTypeName", title: "质检类型" },
          { field: "statusName", title: "状态" },
          { field: "checkUserName", title: "操作员" },
          { field: "checkTime", title: "送检时间" },
          {
            align: "center",
            toolbar: "#purQualityTaskDetailTableBar",
            title: "操作",
            fixed: "right",
            width: 128
          }
        ]
      ],
      done: function (res, curr, count) {
        //移除按钮
      }
    });
    admin.initDataSelectAndMap("production/qualityTesting/admin/qualityTestingCategory/list", { limit: 9999 }, "qualityTestingCategoryId", null, "质检分类", "name", "id", null);
    admin.initDataSelectAndMap("enterprise/purchase/admin/supplierInfoPass/list", { limit: 9999 }, "supplierId", null, "供应商", "corporateName", "id", null);
    laydate.render({
      elem: "#createTime",
      type: "date",
      range: true,
      done: function (value, date, endDate) {
        if (value) {
          var dateValue = value.split(" - ");
          $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
          $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
        } else {
          $("input[name='startCreateTime']").val("");
          $("input[name='endCreateTime']").val("");
        }
      }
    });
    // 工具条点击事件
    table.on("tool(purQualityTaskDetailTable)", function (obj) {
      let data = obj.data;
      if (obj.event === "details") {
        admin.formOpen({
          area: ["90%", "85%"],
          style: "font-size: 20px;",
          title: "录入质检结果",
          content: $("#purQualityTaskCheckForm").html(),
          success: async function () {
            let res = await admin.reqPro("enterprise/purchase/admin/purQualityTaskDetail/detail/" + data.id, {});
            let resData = res.data;
            table.render({
              elem: "#myPurQualityTaskDetailTable2",
              data: data ? [data] : [],
              cols: [
                [
                  { field: "detailCode", title: "IQC取样样品编号" },
                  { field: "taskCode", title: "IQC任务编号" },
                  { field: "orderCode", title: "物流记录编号" },
                  { field: "orderCode", title: "单据编号" },
                  { field: "supplierName", title: "供应商" },
                  { field: "skuCode", title: "料号" },
                  { field: "skuName", title: "品名" },
                  { field: "qualityTestingCategoryName", title: "质检分类" },
                  { field: "qualityTestingTypeName", title: "质检性质" },
                  { field: "qcQualityTypeName", title: "质检类型" },
                  { field: "statusName", title: "状态" },
                  { field: "checkUserName", title: "操作员" },
                  { field: "checkTime", title: "送检时间" }
                ]
              ]
            });
            table.render({
              elem: "#purQualityTaskCheckResultTable2",
              data: resData.detailValues,
              cols: [
                [
                  { type: "numbers", title: "序号" },
                  { field: "qualityName", title: "参数项目名称" },
                  { field: "no", title: "参数编码" },
                  { field: "way", title: "检测方式" },
                  { field: "utensil", title: "检测器具" },
                  { field: "importance", title: "必检项" },
                  { field: "standard", title: "质检标准" },
                  { field: "trueValue", title: "实测值", templet: admin.setTableInput("trueValue", true) },
                  {
                    field: "qualityValue",
                    title: "判定结果",
                    templet: admin.setTableSelect(
                      "qualityValue",
                      [
                        { value: "1", label: "合格" },
                        { value: "2", label: "不合格" }
                      ],
                      true,
                      "qualityValue"
                    )
                  },
                  { field: "remark", title: "备注", templet: admin.setTableInput("remark", true) }
                ]
              ]
            });
          }
        });
      }
    });

    //监听排序
    table.on("sort(purQualityTaskDetailTable)", function (obj) {
      table.reload("purQualityTaskDetailTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type
        }
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
      return false;
    });

    // 添加按钮点击事件
    $("#purQualityTaskDetailBtnAdd").click(function () {
      showQualityInspectionTaskModel();
    });

    // 添加质检任务弹窗 && 质检任务详情弹窗
    function showQualityInspectionTaskModel(data, _type) {
      //如果是详情 或者是 修改
      if (data) {
        data.type = _type;
        admin.putTempData("workOrderQuaTaskInfoData", data);
      } else {
        admin.putTempData("workOrderQuaTaskInfoData", null);
      }
      if (data) {
      } else {
        title = "添加IQC质检任务";
        admin.formOpen({
          type: 1,
          area: ["80%", "85%"],
          style: "font-size: 20px;",
          title: title,
          path: "components/purchase/purQuality/purQualityTaskDetailForm.html",
          finish: function () {
            table.reload(insTb, {});
          }
        });
      }
    }
  });
</script>
